package cn.fulong.frame.service.impl;

import cn.fulong.frame.config.Platform;
import cn.fulong.frame.service.ComputeData;
import cn.fulong.frame.service.GetData;
import cn.fulong.frame.service.ViewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("viewService")
public class ViewServiceImpl implements ViewService {

    @Autowired
    @Qualifier("computeDataByExtend")
    private ComputeData computeDataByExtend;

    @Override
    public Map getPageData(String code, Map args) throws Exception {
        // 获取配置信息
        Map webPage = Platform.getInstance().getWebPage(code);
        if (webPage == null) {
            log.info("没有配置信息：" + code);
            return null;
        }
        Map datas = new HashMap();

        List<Map> datasets = getList(webPage, "dataset");
        for (Map dataset : datasets) {
            String type = dataset.get("type").toString();
            GetData getData = null;
            if (type.equals("sql")) {
                getData = (GetData) Platform.getInstance().getBean("getDataFromSQL");
            } else if (type.equals("cache")) {
                getData = (GetData) Platform.getInstance().getBean("getDataFromCache");
            } else if (type.equals("solr")) {
                getData = (GetData) Platform.getInstance().getBean("getDataFromSolr");
            } else if (type.equals("mongoDB")) {
                getData = (GetData) Platform.getInstance().getBean("getDataFromMongoDB"); //getDataFromMongoDB;
            }

            //System.out.println(DateTimeUtil.getCurrentLongDateTimeStr());
            Map data = getData.execute(dataset, args);
            //System.out.println(DateTimeUtil.getCurrentLongDateTimeStr());
            if (null != data) {
                datas.putAll(data);
            }
        }

        // 数据集计算
        List<Map> computes = getList(webPage, "compute");
        if (computes == null || computes.size() == 0) {
            return datas;
        }
        for (Map compute : computes) {
            String type = compute.get("type").toString();
            ComputeData computeData = null;
            if (type.equals("extend")) {
                // 两个结果集扩展合并
                computeData = (ComputeData) Platform.getInstance().getBean("computeDataByExtend");
            } else if (type.equals("tree")) {
                // 将结果集从列表转为树状层级关系
                computeData = (ComputeData) Platform.getInstance().getBean("computeDataByTree");
            } else if (type.equals("group")) {
                // 将结果集按某个字段分组
                computeData = (ComputeData) Platform.getInstance().getBean("computeDataByGroup");
            }
            Map data = computeData.execute(datas, compute);
            datas.putAll(data);
        }

        return datas;
    }

    private List<Map> getList(Map webPage, String element) {
        if (null == webPage.get(element + "s")) {
            return null;
        }
        Object obj = ((Map) webPage.get(element + "s")).get(element);
        List<Map> list = new ArrayList();
        if (obj instanceof ArrayList) {
            list = (ArrayList) obj;
        } else {
            list.add((Map) obj);
        }
        return list;
    }
}
